// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>

void test_signed_char()
{
	char a[1000];
	int i=0;
	for(i=0; i<1000; i++)
	{
		a[i]=-1-i;
	}
	printf("strlen(a)=%d\n", strlen(a));
}

void test_signed_add_unsigned()
{
	int i=-20;
	unsigned j=10;
	printf("-20+10=%d\n", i+j);
}

void test_union(void)
{
	union {
		int i;
		char c[2];
	}*p, u;

	p = &u;
	memset(&u, 0, sizeof(u));
	p->c[0] = 0x39;
	p->c[1] = 0x38;
	
	printf("p->i=0x%x\n", p->i);
}

void check_cpu(void)
{
	union {
		int i;
		char c;
	}u;
	u.i = 1;
	
	if(u.c == 0)
		printf("Big endian\n");
	else
		printf("Little endian\n");
}

void test_pointer(void)
{
	int a[5]={1,2,3,4,5};
	int *ptr1=(int *)(&a+1);
	int *ptr2=(int *)((int)a+1);
	int *ptr3=(int *)(a+1);
	printf("ptr1[-1]=0x%08x, *ptr2=0x%08x, *ptr3=0x%08x\n",ptr1[-1],*ptr2, *ptr3);

	char c[5] = {'a','b','c','d'};
	char (*ptr4)[5] = &c;
	char (*ptr5)[5] = &c;

	printf("&c=0x%08x, ptr4+1=0x%08x, *ptr5+1=0x%08x\n", &c, ptr4+1, ptr5+1);
}

void test_enum(void)
{
	enum eColor
	{
		RED = 0,
		GREEN,
		BLUE
	}mycolor;

	mycolor = GREEN;
	printf("sizeof(mycolor)=%d\n", sizeof(mycolor));
}

void test_operater(void)
{
	int i = 0, j = 0;
	char a[10] ={"abcde"};

	if((++i > 0) || (++j > 0))
	{
		;
	}
	printf("i=%d,j=%d\n", i, j);
	printf("0x01<<2+3=%d, 0x01<<2+30=%d, 0x01<<2-3=%d\n", 0x01<<2+3, 0x01<<2+30, 0x01<<2-3);

	i = 3;
	printf("(++i)+(++i)+(++i)=%d\n", (++i)+(++i)+(++i));
	i = 3;
	printf("(i++)+(i++)+(i++)=%d\n", (i++)+(i++)+(i++));
	i = 3;
	j = (i++,i++,i++);
	printf("j = (i++,i++,i++)=%d\n", j);
	i = 3;
	j = (i++)+(i++)+(i++);
	printf("j = (i++)+(i++)+(i++)=%d\n", j);

	i = 0;
	for (i=0, printf("first i=%d\n", i); printf("second i=%d\n", i), i<3; i++, printf("third i=%d\n", i))
	{
		printf("fourth i=%d\n", i);
	}
}

void test_pre_compile(void)
{
#define X 3
#define Y X*2
#undef X
#define X 2

	int z=Y;

	printf("z=%d, __FILE__=%s, __LINE__=%d, __DATE__=%s, __TIME__=%s\n", z, __FILE__, __LINE__, __DATE__, __TIME__);

#pragma message("hzf compile come here")

#define SQR(x) printf("The square of "#x" is %d\n", ((x)*(x)))
	SQR(8);

#define XNAME(n) x##n
	int XNAME(1) = 1;
	int XNAME(2) = 2;

	printf("x1=%d,x2=%d\n", x1, x2);
#undef XNAME
}

void test_sizeof_add_strlen()
{
	char* str="hello world"; 
	printf("sizeof(str)=%d,strlen(str)=%d\n", sizeof(str),strlen(str));
}

int main(int argc, char* argv[])
{
	test_union();
	check_cpu();
	test_pointer();
	test_enum();
	test_operater();
	test_pre_compile();
	test_sizeof_add_strlen();
	return 0;
}

